import GBC_emulator_core from './runtime/gbc/gbc'

const BG_IMG_SRC = 'images/jsgbc.png'
const screenWidth = window.innerWidth
const screenHeight = window.innerHeight
const ctx = canvas.getContext('2d');
const points = [
  { //A
    x: 325,
    y: 450,
    w: 60,
    h: 60
  },
  { //B
    x: 255,
    y: 455,
    w: 60,
    h: 60
  },
  { //SELECT
    x: 150,
    y: 610,
    w: 40,
    h: 20
  },
  { //START
    x: 220,
    y: 610,
    w: 40,
    h: 20
  },
  { //UP
    x: 60,
    y: 440,
    w: 40,
    h: 50
  },
  { //DOWN
    x: 60,
    y: 500,
    w: 40,
    h: 50
  },
  { //LEFT
    x: 25,
    y: 475,
    w: 50,
    h: 40
  },
  { //RIGHT
    x: 90,
    y: 475,
    w: 50,
    h: 40
  }
];

/**
 * 游戏主函数
 */
export default class Main {
  constructor() {
    let img = new Image()
    img.src = BG_IMG_SRC
    img.onload = function () {
      let w = screenWidth > img.width ? img.width : screenWidth;
      let h = img.height * (w / img.width);
      ctx.drawImage(this, 0, 40, w, h);
    }
    this.restart()
  }

  restart() {
    console.log('restart')
    let gbc = new GBC_emulator_core();
    let filePath = '/js/runtime/rom/tetris.gb.ogg';
    let fileSystemManager = wx.getFileSystemManager();
    let self = this;
    fileSystemManager.readFile({
      filePath: filePath,
      success: function (res) {
        gbc.set_rom(res.data, 'tetris');
        wx.onTouchStart(function (evt) {
          let point = self.getPoint(evt,points)
          switch (point){
            case 0:
              gbc.buttons.a = true
              break;
            case 1:
              gbc.buttons.b = true
              break;
            case 2:
              gbc.buttons.select = true
              break;
            case 3:
              gbc.buttons.start = true
              break;
            case 4:
              gbc.buttons.up = true
              break;
            case 5:
              gbc.buttons.down = true
              break;
            case 6:
              gbc.buttons.left = true
              break;
            case 7:
              gbc.buttons.right = true
              break;
          }
        });
        wx.onTouchEnd(function (evt) {
          let point = self.getPoint(evt,points)
          switch (point){
            case 0:
              gbc.buttons.a = false
              break;
            case 1:
              gbc.buttons.b = false
              break;
            case 2:
              gbc.buttons.select = false
              break;
            case 3:
              gbc.buttons.start = false
              break;
            case 4:
              gbc.buttons.up = false
              break;
            case 5:
              gbc.buttons.down = false
              break;
            case 6:
              gbc.buttons.left = false
              break;
            case 7:
              gbc.buttons.right = false
              break;
          }
        })
      }
    })
  }
  getPoint(evt, points) {
    let mx = parseInt(evt.changedTouches[0].clientX);
    let my = parseInt(evt.changedTouches[0].clientY);
    //console.log("mx:"+mx+"  my:"+my)
    let point;
    if (mx >= points[0].x && mx <= points[0].x + points[0].w &&
      my >= points[0].y && my < points[0].y + points[0].h) {
      point = 0
    } else if (mx >= points[1].x && mx <= points[1].x + points[1].w &&
      my >= points[1].y && my < points[1].y + points[0].h) {
      point = 1
    } else if (mx >= points[2].x && mx <= points[2].x + points[2].w &&
      my >= points[2].y && my < points[2].y + points[2].h) {
      point = 2
    } else if (mx >= points[3].x && mx <= points[3].x + points[3].w &&
      my >= points[3].y && my < points[3].y + points[3].h) {
      point = 3
    } else if (mx >= points[4].x && mx <= points[4].x + points[4].w &&
      my >= points[4].y && my < points[4].y + points[4].h) {
      point = 4
    } else if (mx >= points[5].x && mx <= points[5].x + points[5].w &&
      my >= points[5].y && my < points[5].y + points[5].h) {
      point = 5
    } else if (mx >= points[6].x && mx <= points[6].x + points[6].w &&
      my >= points[6].y && my < points[6].y + points[6].h) {
      point = 6
    } else if (mx >= points[7].x && mx <= points[7].x + points[7].w &&
      my >= points[7].y && my < points[7].y + points[7].h) {
      point = 7
    } else{
      point = 15
    }
    return point
  }
}